package com.huangliusong.lengthoflongestsubstring;

import java.util.HashSet;
import java.util.Set;

/**
 * @author huangliusong
 * @since 2019/2/21
 * {@link }
 */
public class Solution2 {
    public int lengthOfLongestSubstring(String s) {
        int n=s.length();
        Set<Character> set=new HashSet<Character>();
        int ans=0,i=0,j=0;
        while(i<n&&j<n){
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
               // System.out.println(set.toString());
                set.remove(s.charAt(i++));
             //   System.out.println(set.toString());
            }
        }
        return ans;
    }
}
